package vma.nicici.chat.api;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import vma.nicici.chat.communication.AsynchCallback;
import vma.nicici.chat.communication.GetHttpRequest;
import vma.nicici.chat.communication.GsonHttp;
import vma.nicici.chat.communication.Parametrizable;
import vma.nicici.chat.communication.PostHttpRequest;
import vma.nicici.chat.gson.Response;
import android.util.Log;

import com.facebook.Session;
import com.facebook.model.GraphUser;

public class Api {
	public static final String URL = "http://lu-pa.sk/vma2012/api/";
	public static final String KADEK_KEY = "810b40c7a7d1701240c8f03e6a44ec0dacceecf4";
	public static final String JOZO_KEY = "7c176a316e83e7b9a5914f1717eef51475fbcaaa";
	
	private GraphUser user = null;
	private Session session = null;
	private final List<NameValuePair> sharedParams = new ArrayList<NameValuePair>();
	
	/* pouzivat sa bude cca takto:
	 *  Api.pullResponseAsynch(new GetRequestParameters(-1), new callback (){ });
	 */
	
	
	public class PostRequestParameters implements Parametrizable {
		protected String lastPullTimestamp;
		protected String messageText;
		protected double lat;
		protected double lon;
		
		public PostRequestParameters(String lastPullTimestamp, String messageText, double lat, double lon) {
			this.lastPullTimestamp = lastPullTimestamp;
			this.messageText = messageText;
			this.lat = lat;
			this.lon = lon;
		}
		
		public List<? extends NameValuePair> asParameters() {
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair(Protocol.LAST_PULL_TIMESTAMP, lastPullTimestamp));
			
			if (user != null)
			  params.add(new BasicNameValuePair(Protocol.FACEBOOK_USER_ID, user.getId()));
			
			if (session != null)
		  	  params.add(new BasicNameValuePair(Protocol.FACEBOOK_TOKEN, session.getAccessToken()));
			
			params.add(new BasicNameValuePair(Protocol.MESSAGE_TEXT, messageText));
			params.add(new BasicNameValuePair(Protocol.LATITUDE, lat+""));
			params.add(new BasicNameValuePair(Protocol.LONGITUDE, lon+""));
			return params;
		}
	}
	
	public class GetRequestParameters implements Parametrizable {
		protected String lastPullTimestamp;
		
		public GetRequestParameters(String lastPullTimestamp) {
			this.lastPullTimestamp = lastPullTimestamp;
		}
		
		public List<? extends NameValuePair> asParameters() {
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair(Protocol.LAST_PULL_TIMESTAMP, lastPullTimestamp));
			return params;
		}
	}
	
	
	public Api() {
		sharedParams.add(new BasicNameValuePair(Protocol.KEY, KADEK_KEY));
	}
	
	public void setUser(GraphUser u) {
		user = u;
	}
	
	public GraphUser getUser() {
		return user;
	}
	
	public boolean isLogged() {
		return user != null && session != null;
	}
	
	public void setSession(Session s) {
		session = s;
	}
	
	public Session getSession() {
		return session;
	}
	
	public List<NameValuePair> getSharedParameters() {
		return new ArrayList<NameValuePair>(sharedParams);
	}
	
	
	public void pullResponseAsynch(final GetRequestParameters get, final AsynchCallback<Response> callback) {
		Thread t = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
				    Response r = pullResponse(get);
					callback.onFinished(r);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		t.start();
	}
	
	private Response pullResponse(GetRequestParameters get) {
		List<NameValuePair> params = getSharedParameters();
		params.addAll(get.asParameters());
		
		try {
			GetHttpRequest request = new GetHttpRequest(URL, params);
			String responseString = request.getStringResponse();
			Log.i("get response", responseString);
			Response response = GsonHttp.convert(responseString);
			return response;
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
		return null;
	}
	
	
	public void postResponseAsynch(final PostRequestParameters post, final AsynchCallback<Response> callback) {
		Thread t = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
				    Response r = postResponse(post);
					callback.onFinished(r);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		t.start();
	}
	
	public Response postResponse(PostRequestParameters post) {
		List<NameValuePair> params = getSharedParameters();
		params.addAll(post.asParameters());

		try {
			PostHttpRequest request = new PostHttpRequest(URL, params);
			String responseString = request.getStringResponse();
			
			for (NameValuePair pr : params) {
				Log.i("post response param", pr.getName() + ": " + pr.getValue());
			}
			
			Log.i("post response", responseString);
			
			
			Response response = GsonHttp.convert(responseString);
			return response;
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
		return null;
	}

	
	
}
